<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/22
 * Time: 11:12
 */

namespace Home\Controller;


use Think\Auth;

class DecorateController extends CommonController
{
    public function index(){

        $this->display();
    }

    //新增装饰分类
    public function add_product_decorate(){
        $name=$_POST['name'];
        $data=array(
            'name'=>$name,
            'ac_time'=>time()
        );
        $res=D('decorate')->add($data);
        if(!$res){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }

    //删除装饰分类
    public function del_next(){
        $id=$_POST['id'];
        //先查询子分类
        $zi_ne=D('decorate_next')->where(array('pid'=>$id))->select();
        if($zi_ne){
            foreach($zi_ne as $key=>$value){
                $photo_list=D('product_next_pic')->where(array('pid'=>$value['id']))->select();
                if($photo_list){
                    foreach($photo_list as $k=>$v){
                        $thumb='./Uploads/Cms/productPic/'.$v['thumb'];
                        $pic='./Uploads/Cms/productPic/'.$v['pic'];
                        if(is_file($thumb)&&is_file($pic)){
                            unlink($thumb);unlink($pic);
                        }
                        $re_ne_pic=D('product_next_pic')->delete($v['id']);
                    }
                }
                $re_ne=D('decorate_next')->delete($value['id']);
            }
        }
        $res=D('decorate')->delete($id);
        if(!$res){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }

    //获取列表
    public function get_decorate_list(){
        $count=D('decorate')->count();
        $list=D('decorate')->field("*,FROM_UNIXTIME(ac_time) as ac_time")->limit($_GET['offset'],$_GET['limit'])->order('id desc')->select();
        echo  json_encode(array('rows'=>$list,'total'=>$count));
    }
    //查看子类
    public function get_detail_list(){
        $id=$_GET['id'];
        $this->assign('id',$id);
        import('ORG.Util.Auth');//加载类库
        $auth=new Auth();
        if(!$auth->check('Home/Decorate/upload_product_file',$_SESSION['user']['id'])){
            $this->assign('dis','no');
        }else{
            $this->assign('dis','yes');
        }
        $this->display();
    }

    //新增子类
    public function add_decorate(){
        $name=$_POST['name'];
        $pid=$_POST['pid'];
        $data=array(
            'name'=>$name,
            'pid'=>$pid,
            'ac_time'=>time()
        );
        $res=D('decorate_next')->add($data);
        if(!$res){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }

    //查询子类
    public function get_decorate_next_list(){
        $count=D('decorate_next')->where(array('pid'=>$_GET['pid']))->count();
        $list=D('decorate_next')->where(array('pid'=>$_GET['pid']))->field("*,FROM_UNIXTIME(ac_time) as ac_time")->limit($_GET['offset'],$_GET['limit'])->order('id desc')->select();
        echo  json_encode(array('rows'=>$list,'total'=>$count));
    }

    //上传装饰图片
    public function upload_product_file(){
        $id=$_GET['id'];
        $import=new \Think\Upload(C('ProductConfig'));//实例化上传类
        $info=$import->upload();//图片上传操作
        if(!$info){
            $this->ajaxReturn(array('success' =>'error'));
        }
        //生成缩略图
        $thumb= new \Think\Image();
        $pic=$info[0]['savepath'].$info[0]['savename'];
        $uploadSrc=C('ProductConfig')['rootPath'].$pic;
        $thumb->open($uploadSrc);
        $thumb->thumb(250, 250)->save(C('ProductConfig')['rootPath'].$info[0]['savepath'].'thumb_'.$info[0]['savename']);
        $data=array();
        $data['pid']=$id;
        $data['pic']=$pic;
        $data['thumb']=$info[0]['savepath'].'thumb_'.$info[0]['savename'];
        $data['ac_time']=time();
        $id=D('product_next_pic')->add($data);
        if(!$id){
            $this->ajaxReturn(array('success' =>'error'));
        }
        $this->ajaxReturn(array('success' =>'success'));
    }

    //查看图片
    public function get_pic_data(){
        $id=$_GET['id'];
        $data=D('product_next_pic')->where(array('pid'=>$id))->select();
        $row=array();
        $row['title']='产品图';
        $row['id']=$id;
        $row['start']=0;
        foreach($data as $key=>$value){
            $i=$key+1;
            $res[]=array(
                'alt'=>'第'.$i.'张',
                'pid'=>$value['id'],
                'src'=>'/'.'Uploads/Cms/productPic/'.$value['pic'],
                'thumb'=>'/'.'Uploads/Cms/productPic/'.$value['thumb']
            );
        }
        $row['data']=$res;
        echo json_encode($row);
    }
    //查看列表
    public function get_detail_nex_list(){
        $id=I('get.id');
        $name=D('decorate_next')->find($id);
        $this->assign('id',$id);
        $this->assign('name',$name['name']);
        $this->display();
    }

    //查看
    public function get_decorate_next_w_list(){
        $id=$_GET['id'];
        $count=D('product_next_pic')->where(array('pid'=>$id))->count();
        $list=D('product_next_pic')->where(array('pid'=>$id))->limit($_GET['offset'],$_GET['limit'])->order('id desc')->select();
        foreach($list as $key=>$value){
            $list[$key]['thumb_url']=__ROOT__.'/'.'Uploads/Cms/productPic/'.$value['thumb'];
        }
        echo json_encode(array('rows'=>$list,'total'=>$count));
    }

    //单张删除
    public function del_this(){
        $info=D('product_next_pic')->find($_GET['id']);
        $thumb='./Uploads/Cms/productPic/'.$info['thumb'];
        $pic='./Uploads/Cms/productPic/'.$info['pic'];
        if(is_file($thumb) && is_file($pic)){
            unlink($thumb);unlink($pic);
            $del=D('product_next_pic')->delete($_GET['id']);
            if(!$del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误1'));
        }
    }

    //删除子类
    public function del_next_one(){
        $id=$_POST['id'];
        $photo_list=D('product_next_pic')->where(array('pid'=>$id))->select();
        if($photo_list){
            foreach($photo_list as $k=>$v){
                $thumb='./Uploads/Cms/productPic/'.$v['thumb'];
                $pic='./Uploads/Cms/productPic/'.$v['pic'];
                if(is_file($thumb)&&is_file($pic)){
                    unlink($thumb);unlink($pic);
                }
                $re_ne_pic=D('product_next_pic')->delete($v['id']);
            }
        }
        $del=D('decorate_next')->delete($id);
        if(!$del){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }
}